# -*- coding:utf-8 -*-
from fastapi import Request
from app.entity.system import SysPost
from core.curd import CRUDBase, IBaseDao
from sqlalchemy.ext.asyncio import AsyncEngine
from core.constants import DBConstants
from sqlalchemy import select


class SysPostDao(IBaseDao):
    """
    测试模块服务层
    """

    def __init__(self, req: Request):
        self.db_session: AsyncEngine = req.app.state.db_async_session
        self.base = CRUDBase(SysPost)

    async def select_options(self):
        data = await self.query(select(SysPost.id, SysPost.post_name).filter(SysPost.status == DBConstants.STATUS_OK))
        return list(map(lambda x: {"id": x[0], "name": x[1]}, data))

    @classmethod
    async def instance(cls, req: Request):
        return cls(req)
